package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.ActivityCourseDao;
import com.java.domain.ActivityCourse;
import com.java.domain.event.ActivityCourseEvent;
import com.java.service.CoachService;
import com.java.service.CourseService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class CourseServiceImpl implements CourseService {

	@Resource ActivityCourseDao courseDao;
	@Resource CoachService coachService;
	
	@Override
	public TableRespone getList(int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("courseDel", 0);
		List<ActivityCourseEvent> list = courseDao.selectAll(params, page, size);
		if(!MyTools.isEmpty(list)) {
			for (ActivityCourseEvent course : list) {
				course.setCoach(coachService.getOne(course.getCourseCoachId()));
			}
		}
		long total = courseDao.selectAllCount(params);
		return new TableRespone(list, total);
	}

	@Override
	public ActivityCourseEvent getOne(String courseId) {
		ActivityCourseEvent info = courseDao.selectByKey(courseId);
		if(null != info) {
			info.setCoach(coachService.getOne(info.getCourseCoachId()));
		}
		return info;
	}

	@Override
	public BaseRespone saveCourse(ActivityCourse course) {
		int row = 0;
		ActivityCourse info = courseDao.selectByKey(course.getCourseId());
		if(null == info) {
			info = new ActivityCourse()
			.setCourseId(MyTools.getUUID())
			.setCourseDel(0)
			.setCourseTime(new Date())
			.setCourseCoachId(course.getCourseCoachId())
			.setCourseTitle(course.getCourseTitle());
			row = courseDao.insertSelective(info);
		}else {
			info.setCourseCoachId(course.getCourseCoachId())
			.setCourseTitle(course.getCourseTitle());
			row = courseDao.updateSelective(info);
		}
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone delCourse(String courseId) {
		int row = courseDao.deleteUpdate(courseId, "course_del");
		return BaseRespone.result(row, "Success");
	}

}
